import {Component, ViewChild} from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  Events,
  AlertController,
  App,
  ModalController,
  NavController,
  NavParams,
  ActionSheetController,
  Content,
  ViewController
} from 'ionic-angular';

import {Config} from '../../../providers/config';
import { Common } from '../../../providers/common';

import _ from 'lodash';
import {ContactChoosePage} from "../../contact/choose/choose.component";

@Component({
  selector: 'page-email-query',
  templateUrl: './query.html'
})
export class EmailQueryPage {
	moa_page_name: any = "page-email-query";
  moa_page_explain: any = "邮件 - 高级查询";

  condition = null;

  //默认查询条件
  defaultCondition = {
    "keyWord" : "",
    "addresser_id" : null,
    "addresser" : null,
    "recipients_id" : null,
    "recipients" : null,
    "startDate" : null,
    "endDate" : null
  };

  defaultEmpByTree = [];

  //可以选择的最大日期
  maxDate = "";
  //结束日期可以选择的最小日期
  minEndDate = "1970-01-01";

  //是否显示发件人
  showAddresser = true;
  //是否显示收件人
  showRecipients = true;

  showAddresserGroup = ["IMailInbox", "WasteBox", "IMailTask", "IMailImp", "IMailHIS"];
  showRecipientsGroup = ["DraftBox", "IMOutbox", "IMailHIS"];

  constructor(public alertCtrl: AlertController,
              public app: App,
              public modalCtrl: ModalController,
              public navCtrl: NavController,
              private navParams: NavParams,
              public events: Events,
              public config: Config,
              public actionSheetCtrl: ActionSheetController,
              public viewCtrl: ViewController,
              public storage: Storage,
              public common: Common) {
      //可以选择的日期不能超过今天
      this.maxDate = this.common.getNowFormatDate();
      this.condition = this.defaultCondition;
      this.storage.get("advancedQueryCondition")
        .then(data => {
            console.log("test" + data);
            if (data != null) {
                this.condition = data;
            }
        });

      let listType = this.navParams.get("listType");
      if (this.showAddresserGroup.indexOf(listType) == -1) {
          this.showAddresser = false;
      }
      if (this.showRecipientsGroup.indexOf(listType) == -1) {
          this.showRecipients = false;
      }
      console.log(this.condition);
  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  /**
   * [chooseEmp 选择人员]
   * @param {[type]} item [description]
   */
  chooseEmp(type) {
    console.log("##authorization.detail.chooseEmpnames 选择外出人员");
    let that = this;
    let modal = this.modalCtrl.create(ContactChoosePage, {
      selected: this.defaultEmpByTree,
      isSelectUser: true,
      isSelectOne: true
    });

    modal.present();
    modal.onDidDismiss((data: any[]) => {
      console.info(data);
      if (data) {
        that.defaultEmpByTree = data;
        switch (type) {
          case "addresser":
            this.condition.addresser_id = _.toString(_.map(data, "empid")); // 外出人员用empid, 流程节点选择人用loginid
            this.condition.addresser = _.toString(_.map(data, "name"));
            break;
          case "recipients":
            this.condition.recipients_id = _.toString(_.map(data, "empid")); // 外出人员用empid, 流程节点选择人用loginid
            this.condition.recipients = _.toString(_.map(data, "name"));
            break;
        }
      }
    });
  }

  ionChange(){
      //设置结束日期的最小可选日期等于起始日期
      this.minEndDate = this.condition.startDate;
  }

  /**
   * 保存搜索条件并关闭Modal返回List页面加载搜索结果
   */
  search() {
      this.storage.set("advancedQueryCondition", this.condition);
      this.viewCtrl.dismiss("true");
  }

  /**
   * 清空查询条件
   */
  clear() {
      this.condition = this.defaultCondition;
      this.storage.remove("advancedQueryCondition");
  }

}


